package com.yitong.equipment.ui.adapter.courseprovider

import android.view.View
import com.chad.library.adapter.base.provider.BaseNodeProvider
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import androidx.core.view.ViewCompat
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yitong.equipment.R
import com.yitong.equipment.constant.EventIds
import com.yitong.equipment.entity.other.Event
import com.yitong.equipment.utils.EventBusUtil

class FirstProvider : BaseNodeProvider() {
    override val itemViewType: Int
        get() = 1
    override val layoutId: Int
        get() = R.layout.item_node_first

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        val entity = data as FirstData
        helper.setText(R.id.title, entity.title)
        helper.setImageResource(R.id.iv, R.drawable.icon_back)
        setArrowSpin(helper, data, false)
    }

    private fun setArrowSpin(helper: BaseViewHolder, data: BaseNode, isAnimate: Boolean) {
        val entity = data as FirstData
        val imageView = helper.getView<ImageView>(R.id.iv)
        if (entity.isExpanded) {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200).setInterpolator(DecelerateInterpolator()).rotation(90f).start()
            } else {
                imageView.rotation = 90f
            }
        } else {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200).setInterpolator(DecelerateInterpolator()).rotation(90f).start()
            } else {
                imageView.rotation = 270f
            }
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        val entity = data as FirstData
        if (entity.isExpanded) {
            getAdapter()!!.collapse(position,true)
        } else {
            getAdapter()!!.expand(position,true)
        }

        EventBusUtil.sendEvent(Event<Any>(EventIds.UPDATE_RECYCLER_HEIGHT))
//        getAdapter()?.expandOrCollapse(position, true, true)
    }

}